﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;


namespace Autofocus
{
    class Stepper
    {
        private System.IO.Ports.SerialPort serialPort1;
        public Boolean Connected;
        public Int32 Timeout;

        public Stepper()
        {
            Connected = false;
            Timeout = 5000;

        }

        ~Stepper()
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }

        public Boolean Connect(String PortName, Int32 BaudRate)
        {
            if (Connected)
            {
                return true;
            }
            else
            {
                serialPort1 = new System.IO.Ports.SerialPort();
                serialPort1.PortName = PortName;
                serialPort1.BaudRate = BaudRate;

                try
                {
                    serialPort1.Open();
                }
                catch
                {
                    MessageBox.Show("Unable to open serial port!");
                    return false;
                }
                finally
                {
                    if (serialPort1.IsOpen)
                    {
                        Connected = true;
                    }
                }
                return true;
            }
        }

        public Boolean Disconnect()
        {
            if (Connected && serialPort1.IsOpen)
            {

                try
                {
                    serialPort1.Close();
                    Connected = false;
                }
                catch
                {
                    return false;
                }
                return true;
            }
            else
                return false;
        }

        private Int32 Move(Char motor, Char err_char, Int32 steps)
        {
            if (serialPort1.IsOpen)
            {
                short command = Convert.ToInt16(steps);
                byte[] byteArray = BitConverter.GetBytes(command);

                //const string formatter = "{0,10}{1,13}";
                //Debug.WriteLine( formatter, command, BitConverter.ToString( byteArray ) );

                serialPort1.DiscardInBuffer();
                serialPort1.Write(motor.ToString());
                serialPort1.Write(byteArray, 0, 2);

                // wait for response from motor to confirm it is done
                Int32 BytesToRead = serialPort1.BytesToRead;
                Int32 ElapsedTime = 0;

                while (BytesToRead == 0)
                {
                    if (ElapsedTime < Timeout)
                    {
                        System.Threading.Thread.Sleep(100);
                        ElapsedTime += 100;
                        //Debug.WriteLine("Wating for response");
                        BytesToRead = serialPort1.BytesToRead;
                    }
                    else
                        return 1; 
                }

                //MessageBox.Show(serialPort1.ReadLine().ToString());

                Char char_read = (Char)serialPort1.ReadChar();
                if (char_read == err_char)
                {
                    return 2;
                }
                else if (char_read == motor)
                    return 0;
                else
                    return 1;
            }
            else
                return 1;
        }

        public Int32 MoveX(Int32 step)
        {
            return Move('X', 'A', step);
        }

        public Int32 MoveY(Int32 step)
        {
           return Move('Y', 'B', step);
        }

        public Int32 MoveZ(Int32 step)
        {
            return Move('Z', 'C', step);
        }











    }
}
